home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / cache-scratch.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-10  |  4.2 KB  |  144 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20. /*
  21.   cache-scratch.cpp
  22.   ------------------------------------------------------------------------
  23.   cache-scratch is a benchmark that exercises a heap's cache-locality.
  24.   An allocator that allows multiple threads to re-use the same small
  25.   object (possibly all in one cache-line) will scale poorly, while
  26.   an allocator like Hoard will exhibit near-linear scaling.
  27.   ------------------------------------------------------------------------
  28.   @(#) $Id: cache-scratch.cpp,v 1.11 2000/02/10 19:57:23 emery Exp $
  29.   ------------------------------------------------------------------------
  30.   Emery Berger                    | <http://www.cs.utexas.edu/users/emery>
  31.   Department of Computer Sciences |             <http://www.cs.utexas.edu>
  32.   University of Texas at Austin   |                <http://www.utexas.edu>
  33.   ========================================================================
  34. */
  35.  
  36. /* Try the following (on a P-processor machine):
  37.  
  38.    cache-scratch 1 1000 1 1000000
  39.    cache-scratch P 1000 1 1000000
  40.  
  41.    cache-scratch-hoard 1 1000 1 1000000
  42.    cache-scratch-hoard P 1000 1 1000000
  43.  
  44.    The ideal is a P-fold speedup.
  45. */
  46.  
  47.  
  48. #include "arch-specific.h"
  49. #include "timer.h"
  50.  
  51. #include <iostream.h>
  52. #include <stdlib.h>
  53.  
  54. // This class just holds arguments to each thread.
  55. class workerArg {
  56. public:
  57.   workerArg (char * obj, int objSize, int repetitions, int iterations)
  58.     : _object (obj),
  59.       _objSize (objSize),
  60.       _iterations (iterations),
  61.       _repetitions (repetitions)
  62.   {}
  63.  
  64.   char * _object;
  65.   int _objSize;
  66.   int _iterations;
  67.   int _repetitions;
  68. };
  69.  
  70.  
  71. extern "C" void * worker (void * arg)
  72. {
  73.   // free the object we were given.
  74.   // Then, repeatedly do the following:
  75.   //   malloc a given-sized object,
  76.   //   repeatedly write on it,
  77.   //   then free it.
  78.   workerArg * w = (workerArg *) arg;
  79.   delete w->_object;
  80.   for (int i = 0; i < w->_iterations; i++) {
  81.     // Allocate the object.
  82.     char * obj = new char[w->_objSize];
  83.     // Write into it a bunch of times.
  84.     for (int j = 0; j < w->_repetitions; j++) {
  85.       for (int k = 0; k < w->_objSize; k++) {
  86.     obj[k] = (char) k;
  87.     volatile char ch = obj[k];
  88.     ch++;
  89.       }
  90.     }
  91.     // Free the object.
  92.     delete [] obj;
  93.   }
  94.   delete w;
  95.   return NULL;
  96. }
  97.  
  98.  
  99. int main (int argc, char * argv[])
  100. {
  101.   int nthreads;
  102.   int iterations;
  103.   int objSize;
  104.   int repetitions;
  105.  
  106.   if (argc > 4) {
  107.     nthreads = atoi(argv[1]);
  108.     iterations = atoi(argv[2]);
  109.     objSize = atoi(argv[3]);
  110.     repetitions = atoi(argv[4]);
  111.   } else {
  112.     cerr << "Usage: " << argv[0] << " nthreads iterations objSize repetitions" << endl;
  113.     exit(1);
  114.   }
  115.  
  116.   hoardThreadType * threads = new hoardThreadType[nthreads];
  117.   hoardSetConcurrency (hoardGetNumProcessors());
  118.  
  119.   int i;
  120.  
  121.   // Allocate nthreads objects and distribute them among the threads.
  122.   char ** objs = new char * [nthreads];
  123.   for (i = 0; i < nthreads; i++) {
  124.     objs[i] = new char[objSize];
  125.   }
  126.  
  127.   Timer t;
  128.   t.start();
  129.  
  130.   for (i = 0; i < nthreads; i++) {
  131.     workerArg * w = new workerArg (objs[i], objSize, repetitions / nthreads, iterations);
  132.     hoardCreateThread (threads[i], worker, (void *) w);
  133.   }
  134.   for (i = 0; i < nthreads; i++) {
  135.     hoardJoinThread (threads[i]);
  136.   }
  137.   t.stop();
  138.  
  139.   delete [] threads;
  140.   delete [] objs;
  141.  
  142.   cout << "Time elapsed = " << (double) t << " seconds." << endl;
  143. }
  144.